package cn.south.campus.admin.campus.service.impl;

import cn.south.campus.admin.campus.mapper.AdPositionMapper;
import cn.south.campus.admin.campus.model.entity.AdPosition;
import cn.south.campus.admin.campus.model.req.AdPositionReq;
import cn.south.campus.admin.campus.model.vo.AdPositionVO;
import cn.south.campus.admin.campus.service.AdPositionService;
import cn.south.campus.common.model.enums.CommonEnums;
import cn.south.campus.common.utils.AssertUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 广告位表 服务实现类
 * </p>
 *
 * @author chow
 * @since 2024-11-11
 */
@Service
@AllArgsConstructor
public class AdPositionServiceImpl extends ServiceImpl<AdPositionMapper, AdPosition> implements AdPositionService {

    private final AdPositionMapper adPositionMapper;

    @Override
    public void savePos(AdPositionVO adPositionVO) {
        LambdaQueryWrapper<AdPosition> wrapper = new LambdaQueryWrapper<AdPosition>().eq(AdPosition::getPosNo, adPositionVO.getPosNo());
        AssertUtils.isLtE(adPositionMapper.selectCount(wrapper), 0L, CommonEnums.DATA_REPEAT_ERROR);
        AdPosition adPosition = AdPositionVO.convert(adPositionVO);
        adPositionMapper.insert(adPosition);
    }

    @Override
    public void updatePos(AdPositionVO adPositionVO) {
        AdPosition adPosition = adPositionMapper.selectById(adPositionVO.getId());
        AssertUtils.isNotNull(adPosition, CommonEnums.DATA_HAS_NOT_ERROR);
        adPosition.setName(adPositionVO.getName());
        adPosition.setDescription(adPositionVO.getDescription());
        adPositionMapper.updateById(adPosition);
    }

    @Override
    public List<AdPositionVO> listPos(AdPositionReq req) {
        LambdaQueryWrapper<AdPosition> wrapper = new LambdaQueryWrapper<AdPosition>()
                .eq(StringUtils.hasText(req.getPosNo()), AdPosition::getPosNo, req.getPosNo())
                .like(StringUtils.hasText(req.getName()), AdPosition::getName, req.getName());
        List<AdPosition> positionList = adPositionMapper.selectList(wrapper);
        return positionList.stream().map(AdPositionVO::convert).collect(Collectors.toList());
    }

    @Override
    public IPage<AdPositionVO> pagePos(AdPositionReq req) {
        LambdaQueryWrapper<AdPosition> wrapper = new LambdaQueryWrapper<AdPosition>()
                .eq(StringUtils.hasText(req.getPosNo()), AdPosition::getPosNo, req.getPosNo())
                .like(StringUtils.hasText(req.getName()), AdPosition::getName, req.getName());
        Page<AdPosition> positionPage = adPositionMapper.selectPage(req.getPageDto(), wrapper);
        return positionPage.convert(AdPositionVO::convert);
    }
}
